<?php
require_once dirname(__FILE__).'/Builder.php';

class MicroTal_CompilingContext {
	/** @var string */
	public $content;
	/** @var MicroTal_Builder */
	public $prefix;
	/** @var MicroTal_Builder */
	public $sufix;
	/** @var string|bool */
	public $omit;
	function  __construct() {
		$this->prefix = new MicroTal_Builder();
		$this->sufix = new MicroTal_Builder();
		$this->content = '';
		$this->omit = false;
	}
	public function prepend($string) {
		$this->prefix->prependRaw($string);
	}
	public function prependPhp($patterns, $variables = array()) {
		$string = $this->format($patterns, $variables);
		$this->prefix->prependPhp($string);
	}
	public function append($string) {
		$this->sufix->appendRaw($string);
	}
	public function appendPhp($patterns, $variables = array()) {
		$string = $this->format($patterns, $variables);
		$this->sufix->appendPhp($string);
	}

	protected function format($patterns, $variables) {
		if (!is_array($variables)) {
			$variables = array($variables);
		}
		if (!is_array($patterns)) {
			$patterns = array($patterns);
		}
		$from = array();
		$to = array();
		foreach ($variables as $key => $variable) {
			$from[]= '%'.$key;
			$to[]= $variable;
		}
		$code = '';
		foreach($patterns as $pattern) {
			$code .= str_replace($from, $to, $pattern).";\n";
		}
		return $code;
	}
}

